<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/7/3
 * Time: 上午11:40
 */

namespace app\admin\controller;


use app\src\claim\logic\ClaimLogic;
use app\src\insurance\logic\InsuranceLogic;

class Claim extends Admin
{
    /**
     * 理赔公司列表
     */
    public function queryClaim()
    {
        if(IS_AJAX){

            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $result = (new ClaimLogic)->claimList();

            if($result['status']){
                $info = $result['info'];
                $list = [];
                foreach ($info as $val){
                    //获取保险类型名
                    $map = [
                        'id' => $val['insurance_id']
                    ];
                    $result = (new InsuranceLogic)->getInfo($map);
                    if(!$result['status']) $this->error('获取理赔公司详情失败', null, $result['info']);
                    $val['insurance_name'] = $result['info']['name'];
                    $list[] = $val;
                }
                $this->success('获取理赔公司列表成功', null, $list);
            }else{
                $this->error('获取理赔公司列表失败', null, $result['info']);
            }
        }
    }

    /**
     * 添加理赔公司
     */
    public function addClaim()
    {
        if(IS_AJAX){

            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $insurance_id = 5; //暂时车险

            $name = $this->_param('name');
            if(empty($name)) $this->error('理赔公司名称缺失');

            $logo = $this->_param('logo');
            if(empty($logo)) $this->error('理赔公司logo缺失');

            $contact_number= $this->_param('contact_number');
            if(empty($contact_number)) $this->error('理赔公司联系电话缺失');

            $display_order = $this->_param('display_order',0);

            $entity = [
                'insurance_id' => $insurance_id,
                'name' => $name,
                'logo' => $logo,
                'contact_number' => $contact_number,
                'display_order' => $display_order
            ];

            $result = (new ClaimLogic)->addClaim($entity);

            if($result['status']){
                $this->success('添加理赔公司成功', null, $result['info']);
            }else{
                $this->error('添加理赔公司失败', null, $result['info']);
            }
        }
    }

    /**
     * 修改理赔公司
     */
    public function edit()
    {
        if(IS_AJAX){

            if(!$this->isBackAdmin()){
                $this->error('没有权限查看此内容');
            }

            $insurance_id = 5; //暂时车险

            $claim_id = $this->_param('claim_id');
            if(empty($claim_id)) $this->error('理赔公司id缺失');

            $name = $this->_param('name');
            if(empty($name)) $this->error('理赔公司名称缺失');

            $logo = $this->_param('logo');
            if(empty($logo)) $this->error('理赔公司logo缺失');

            $contact_number= $this->_param('contact_number');
            if(empty($contact_number)) $this->error('理赔公司联系电话缺失');

            $display_order = $this->_param('display_order');

            $entity = [
                'insurance_id' => $insurance_id,
                'name' => $name,
                'logo' => $logo,
                'contact_number' => $contact_number
            ];

            if($display_order != '') $entity['display_order'] = $display_order;

            $result = (new ClaimLogic)->editClaim($claim_id, $entity);

            if($result['status']){
                $this->success('修改理赔公司成功', null, $result['info']);
            }else{
                $this->error('修改理赔公司失败', null, $result['info']);
            }
        }
    }

    /**
     * 理赔公司详情
     */
    public function detail()
    {
        if(!$this->isBackAdmin()){
            $this->error('没有权限查看此内容');
        }

        $mall_id = $this->_param('mall_id');
        if(empty($mall_id)) $this->error('商城商家id缺失');

        $result = (new ClaimLogic)->claimDetail($mall_id);

        if($result['status']){
            //获取保险类型名
            $info = $result['info'];
            $map = [
                'id' => $info['insurance_id']
            ];
            $result = (new InsuranceLogic)->getInfo($map);
            if(!$result['status'])$this->error('获取理赔公司详情失败', null, $result['info']);
            $info['insurance_name'] = $result['info']['name'];

            $this->success('获取理赔公司详情成功', null, $info);
        }else{
            $this->error('获取理赔公司详情失败', null, $result['info']);
        }
    }

    /**
     * 删除理赔公司
     */
    public function delete()
    {
        if(!$this->isBackAdmin()){
            $this->error('没有权限查看此内容');
        }

        $claim_id = $this->_param('claim_id');
        if(empty($claim_id)) $this->error('理赔公司id缺失');

        $result = (new ClaimLogic)->pretendDelete(['id' => $claim_id]);

        if($result['status']){
            $this->success('操作成功', null, $result['info']);
        }else{
            $this->error('操作失败', null, $result['info']);
        }
    }
}